package com.jzo2o.foundations.service.impl;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.expcetions.DBException;
import com.jzo2o.common.expcetions.ForbiddenOperationException;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.common.utils.BeanUtils;
import com.jzo2o.common.utils.StringUtils;
import com.jzo2o.foundations.enums.FoundationStatusEnum;
import com.jzo2o.foundations.mapper.ServeMapper;
import com.jzo2o.foundations.model.domain.Region;
import com.jzo2o.foundations.model.domain.Serve;
import com.jzo2o.foundations.model.domain.ServeItem;
import com.jzo2o.foundations.model.dto.request.ServePageQueryReqDTO;
import com.jzo2o.foundations.model.dto.request.ServeUpsertReqDTO;
import com.jzo2o.foundations.model.dto.response.ServeResDTO;
import com.jzo2o.foundations.service.IRegionService;
import com.jzo2o.foundations.service.IServeItemService;
import com.jzo2o.foundations.service.IServeService;
import com.jzo2o.mysql.utils.PageHelperUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 服务表 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-12-01
 */
@Service
public class ServeServiceImpl extends ServiceImpl<ServeMapper, Serve> implements IServeService {

    /**
     * 查询区域服务分页列表
     * @param dto
     * @return
     */
    @Override
    public PageResult<ServeResDTO> page(ServePageQueryReqDTO dto) {
        //多表关联分页查询时，应该使用pageHelper工具类完成
        return PageHelperUtils.selectPage(dto,()->baseMapper.findServeListByRegionId(dto.getRegionId()));
    }

    @Autowired
    private IServeItemService serveItemService;

    @Autowired
    private IRegionService regionService;

    /**
     * 服务新增
     * @param dtoList
     */
    @Override
    public void batchAdd(List<ServeUpsertReqDTO> dtoList) {
        for (ServeUpsertReqDTO dto : dtoList) {
            Long itemId = dto.getServeItemId();

            //1.判断服务项是否存在
            ServeItem item = serveItemService.getById(itemId);
            if (ObjectUtil.isNull(item)) {
                throw new ForbiddenOperationException("服务项不存在");
            }

            //判断服务项是否已经启用
            if (item.getActiveStatus() != FoundationStatusEnum.ENABLE.getStatus()) {
                throw new ForbiddenOperationException("服务项未启用");
            }

            //判断服务项是否已添加过
            Integer count = this.lambdaQuery()
                    .eq(Serve::getRegionId, dto.getRegionId())
                    .eq(Serve::getServeItemId, dto.getServeItemId())
                    .count();

            if (count > 0) {
                throw new ForbiddenOperationException("服务项已被添加过");
            }

            //构建服务对象数据
            Serve serve = BeanUtils.copyBean(dto, Serve.class);
            Region region = regionService.getById(dto.getRegionId());
            serve.setCityCode(region.getCityCode()); //城市编码

            //添加服务数据到表中
            boolean result = this.save(serve);
            if (!result) {
                throw new DBException("添加服务失败");
            }
        }
    }

    /**
     * 跟新区域服务价格
     * @param id
     * @param price
     */
    @Override
    public void updatePrice(Long id, BigDecimal price) {
        //1.判断该服务是否存在
        Integer count = this.lambdaQuery()
                .eq(Serve::getId, id)
                .count();
        if (count == 0) {
            throw new ForbiddenOperationException("该服务不存在");
        }

        //更改服务价格
        boolean result = this.lambdaUpdate()
                .eq(Serve::getId, id)
                .set(Serve::getPrice, price)
                .update();
        if (!result) {
            throw new DBException("更改服务价格失败");
        }
    }

    /**
     * 区域服务上架
     * @param id
     */
    @Override
    public void onSale(Long id) {
        //1.判断该服务是否存在
        Serve serve = this.getById(id);
        if (ObjectUtil.isNull(serve)) {
            throw new ForbiddenOperationException("服务不存在");
        }

        //2.判断服务区域是否存在
        if (StringUtils.isEmpty(serve.getCityCode()) ) {
            throw new ForbiddenOperationException("未选择服务区域");
        }

        //3.判断该服务是否有服务项
        Integer count = serveItemService.lambdaQuery()
                .eq(ServeItem::getId, serve.getServeItemId())
                .count();
        if (count == 0) {
            throw new ForbiddenOperationException("服务的服务项不存在");
        }

        //4.判断服务当前状态
        if (serve.getSaleStatus() == FoundationStatusEnum.ENABLE.getStatus()) {
            throw new ForbiddenOperationException("服务已经启动过");
        }

        //5.上架服务
        boolean result = this.lambdaUpdate()
                .eq(Serve::getId, id)
                .set(Serve::getSaleStatus, FoundationStatusEnum.ENABLE.getStatus())
                .update();
        if (!result) {
            throw new DBException("上架失败");
        }
    }

    /**
     * 删除区域服务
     * @param id
     */
    @Override
    public void deleteById(Long id) {
        //1.判断服务是否存在
        Serve serve = this.getById(id);

        if (ObjectUtil.isNull(serve)) {
            throw new ForbiddenOperationException("服务不存在");
        }

        //2.判断是否在一个区域
        if (serve.getRegionId() == null) {
            throw new ForbiddenOperationException("服务未有区域不能删除");
        }

        //3.判断服务状态
        if (serve.getSaleStatus() != FoundationStatusEnum.INIT.getStatus()) {
            throw new ForbiddenOperationException("服务不是草稿状态不能删除");
        }
        //3.删除区域服务
        boolean result = this.removeById(id);

        if (!result) {
            throw new DBException("删除失败");
        }
    }

    /**
     * 区域服务下架
     * @param id
     */
    @Override
    public void offSale(Long id) {
        //1.判断服务是否存在
        Serve serve = this.getById(id);

        if (ObjectUtil.isNull(serve)) {
            throw new ForbiddenOperationException("服务不存在");
        }


        //2.判断区域是否为空
        if (StringUtils.isEmpty(serve.getCityCode())) {
            throw new ForbiddenOperationException("区域未绑定");
        }

        //3.判断服务状态
        if (serve.getSaleStatus() != FoundationStatusEnum.ENABLE.getStatus()) {
            throw new ForbiddenOperationException("服务还未上架过");
        }


        //4.上架服务
        boolean result = this.lambdaUpdate()
                .eq(Serve::getId, id)
                .set(Serve::getSaleStatus, FoundationStatusEnum.DISABLE.getStatus())
                .update();
        if (!result) {
            throw new ForbiddenOperationException("下架失败");
        }

    }

    /**
     * 区域服务设置热门
     * @param id
     */
    @Override
    public void onHot(Long id) {
        //1.判断服务是否存在
        Serve serve = this.getById(id);
        if (ObjectUtil.isNull(serve)) {
            throw new ForbiddenOperationException("服务不存在");
        }

        //2.判断是否未上架状态
        if (serve.getSaleStatus() != FoundationStatusEnum.ENABLE.getStatus()) {
            throw new ForbiddenOperationException("服务未上架");
        }


        //3.设置热门状态
        boolean result = this.lambdaUpdate()
                .eq(Serve::getId, id)
                .set(Serve::getIsHot, 1)
                .update();
        if (!result) {
            throw new ForbiddenOperationException("设置热门失败");
        }
    }

    /**
     * 区域服务取消热门
     * @param id
     */
    @Override
    public void offHot(Long id) {
        //1.判断服务是否存在
        Serve serve = this.getById(id);
        if (ObjectUtil.isNull(serve)) {
            throw new ForbiddenOperationException("服务不存在");
        }

        //2.取消热门
        boolean result = this.lambdaUpdate()
                .eq(Serve::getId, id)
                .set(Serve::getIsHot, 0)
                .update();
        if (!result) {
            throw new ForbiddenOperationException("取消热门失败");
        }
    }
}
